<template>
  <div class="app-container">
    <el-form ref="form" style="width:25%" :model="prop" label-width="80px">

      <el-form-item label="道具名称">
        <el-input v-model="prop.propsName"></el-input>
      </el-form-item>

      <el-form-item label="道具类型">
        <el-select v-model="prop.type" placeholder="请选择道具类型">
          <el-option label="功能道具" :value="'0'"></el-option>
          <el-option label="材料道具" :value="'1'"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="道具介绍">
        <el-input type="textarea" v-model="prop.description"></el-input>
      </el-form-item>

      <el-form-item label="道具等级">
        <el-input type="number" v-model="prop.level"></el-input>
      </el-form-item>

      <el-form-item label="选择属性">
        <el-select v-model="prop.property" placeholder="请选择属性">
          <el-option label="气血" :value="1"></el-option>
          <el-option label="速度" :value="2"></el-option>
        </el-select>
        <el-button style="margin-left: 20px;position: relative;top: -10px;" type="danger" icon="el-icon-delete" circle @click="delproperty()" />
      </el-form-item>

      <el-form-item label="属性增幅" v-if="prop.property">
        <el-input type="number" v-model="prop.propertyCount"></el-input>
      </el-form-item>

      <el-form-item label="道具图片">

        <div style="display: flex; align-items: baseline">
          <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            class="avatar-uploader"
            action="#">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
          <el-button style="margin-left: 20px;position: relative;top: -10px;" type="danger" icon="el-icon-delete" circle @click="del()" />
        </div>

      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="onsubmit()" style="margin-right:10px">提交</el-button>
        <a :href="'#/prop/index'">
        <el-button>取消</el-button>
        </a>
      </el-form-item>
    </el-form>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: auto;
  display: block;
}
</style>
<script>
import propApi  from '@/api/props'
import axios from 'axios'
export default {

  data() {
    return {
      imageUrl: '',
      prop: {
        level:1
      },
      imagecropperShow: false, // 是否显示上传组件
      imagecropperKey: 0 // 上传组件id
    }
  },
  created() {
      this.imageUrl = this.$attrs.imageurl

      let id = this.$route.query.id
      if(id){
        this.getById(id)
      }
  },
  methods: {
    //根据id获取道具信息
    getById(id){
      propApi.getPropsById(id).then(res => {
        console.log(res.data.props);
        this.prop = res.data.props
        this.imageUrl = this.prop.propsAvatar;
      })
    },
    //点击提交
    onsubmit(){
      if(this.prop.id){
        //修改
        this.updateProps()
      }else{
        //添加
        this.addProps()
      }
    },
    //更新道具
    updateProps(){
      propApi.updateProps(this.prop).then(res => {
          //弹窗提示
          this.$message({
            message: '修改成功',
            type: 'success'
          });

          //跳转
          this.$router.push("/prop/index")

      })
    },
    //添加道具
    addProps(){
      propApi.addProps(this.prop).then(res => {
          //弹窗提示
          this.$message({
            message: '添加成功',
            type: 'success'
          });

          //跳转
          this.$router.push("/prop/index")

      })
    },
    delproperty(){
      delete this.prop.property
      delete this.prop.propertyCount
      this.$emit('url', '')
    },
    //==================图片上传接口start===================================
    del() {
      this.imageUrl = ''
      this.$emit('url', '')
    },

    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = URL.createObjectURL(file.raw)
    },

    beforeAvatarUpload(file) {
      const loading = this.$loading({
        lock: true,
        text: '图片上传中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      const that = this
      var formData = new FormData()
      formData.append('files', file)
      const url = 'https://www.fengshu.press/fileoss/oss'
      axios.post(url, formData).then(res => {
        loading.close()
        res = res.data
        const url = res.data.url
        that.imageUrl = url
        this.prop.propsAvatar = url;
        that.$emit('url', url)
        console.log(that.prop);
      })
      return false
    }
    //==================图片上传接口Ending===================================

  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
